﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PerformanceAgreement : System.Web.UI.UserControl
    {
        private Main main = new Main();
        private SsMng creatby = new SsMng();
        private object _dataItem = null;
        private IPerformanceAgreementBLL CreatPerformanceAgreement = new PerformanceAgreementBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            lblEmpSeq.Text = Session["emp_seq"].ToString();

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblAgreementSeq.Text = DataBinder.Eval(DataItem, "perf_agreement_seq").ToString();
                    txtGoal.Text = DataBinder.Eval(DataItem, "goal_objective").ToString();
                    txtAction.Text = DataBinder.Eval(DataItem, "action_plan").ToString();
                    txtRemark.Text = DataBinder.Eval(DataItem, "remark").ToString();
                }

            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        private void ClearControlData()
        {
            txtGoal.Text = "";
            txtAction.Text = "";
            txtRemark.Text = "";
        }

        private bool CheckNullValue()
        {
            bool bOk = true;

            if (txtGoal.Text == "")
            {
                main.MessageAlert(Page, "กรุณากรอกเป้าหมาย");
                bOk = false;
            }
            else if (txtAction.Text == "")
            {
                main.MessageAlert(Page, "กรุณากรอกข้อมูล");
                bOk = false;
            }

            return bOk;
        }

        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            int? intAgreementSeq = main.ConvertNullInt(lblAgreementSeq.Text);
            lblEmpSeq.Text = Session["emp_seq"].ToString();
            string strQueryString = lblEmpSeq.Text;
            string strGoal = txtGoal.Text;
            string strAction = txtAction.Text;
            string strRemark = txtRemark.Text;

            if (!CheckNullValue())
            {
                return;
            }

            bool CheckValueSubmit = CreatPerformanceAgreement.CheckValueSubmit(intAgreementSeq);
            if (CheckValueSubmit == true)
            {
                CreatPerformanceAgreement.Insert(strQueryString, strGoal, strAction, strRemark, creatby.EmpSeqI);

				main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
            }

            ChangeMode(Main.EditMode.View);
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            int? intAgreementSeq = main.ConvertNullInt(lblAgreementSeq.Text);
            lblEmpSeq.Text = Session["emp_seq"].ToString();
            string strQueryString = lblEmpSeq.Text;
            string strGoal = txtGoal.Text;
            string strAction = txtAction.Text;
            string strRemark = txtRemark.Text;
            if (!CheckNullValue())
            {
                return;
            }
             bool CheckValueSubmit = CreatPerformanceAgreement.CheckValueSubmit(intAgreementSeq);
             if (CheckValueSubmit == true)
             {
                 CreatPerformanceAgreement.Update(strQueryString, strGoal, strAction, strRemark, creatby.EmpSeqI,
												  intAgreementSeq);
				 main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
             }

            ChangeMode(Main.EditMode.View);
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
           
        }
        
        private void ChangeMode(Main.EditMode mode)
        {
            switch (mode)
            {
               case Main.EditMode.AddNew:
                    cmdUpdate.Visible = false;
                    cmdAdd.Visible = true;
                    cmdCancel.Visible = true;

                    break;

                case Main.EditMode.View:
                    cmdAdd.Visible = false;
                    cmdUpdate.Visible = false;
                    cmdCancel.Visible = true;
                    break;

                case Main.EditMode.Edit:
                    cmdAdd.Visible = false;
                    cmdCancel.Visible = true;
                    cmdUpdate.Visible = true;
                    break;
            }
        }

    }
}